﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Xml.XPath;
//using System.Runtime.Serialization;
//using SharePointClient.SPWebService.SiteData;
using SharePointClient.SPWebService.Webs;

namespace SharePointClient
{
    /// <summary>
    /// SharePoint Website
    /// </summary>
    public class Site:WSSBase,IDisposable
    {

        //        public const string WebService = "_vti_bin/SiteData.asmx";
        public const string WebService = "_vti_bin/Webs.asmx";
        private SiteCollection myCollection=null;
        private ListCollection myLists = null;
        private string title="";
        private string description = "";

        Webs site = new Webs();
/*        SiteData site = new SiteData();
        _sWebMetadata sWebMetadata;
        _sWebWithTime[] vWebs;
        _sListWithTime[] vLists;
        _sFPUrl[] vFPUrls;
        string strRoles;
        string[] vRolesUsers;
        string[] vRolesGroups;
*/        

        /// <summary>
        /// SharePoint WebSite
        /// </summary>
        /// <param name="url">Website-Url</param>
        internal Site(Connection connection, Uri url)
            : base(connection, url)
        {
        }
        internal Site(Connection connection, Uri url,string title)
            : base(connection, url)
        {
            this.title = title;
            //Refresh();
        }
        /// <summary>
        /// If the Information is not present jet - load it!
        /// </summary>
        public void Load()
        {
            if (NeedRefresh) Refresh();
        }
        /// <summary>
        /// Refresh Information from Website
        /// </summary>
        public override void Refresh()
        {
           
            site.Url = Uri.ToString() + WebService;
            site.Credentials = Credentials;
            ProgressEventArgs p = myConnection.DoProgress("Loading Site \"" + Uri + "\"");
            string web = Uri.ToString();
            if (web.EndsWith("/")) web = web.Substring(0, web.Length - 1);
            XmlNode x = site.GetWeb(web);
            title = x.Attributes["Title"].Value;
            description = x.Attributes["Description"].Value;
            p.Operation = "Site '" + title + "' loaded.";
            p.State = BeforeOrAfter.After;
            myConnection.UpdateProgress(p);
            NeedRefresh = false;

 
        }

        /// <summary>
        /// Website Title
        /// </summary>
        public string Title
        {
            get
            {
                if (title=="") Load();
                return title;
            }
        }
        /*
        /// <summary>
        /// WebSite Last Change
        /// </summary>
        public DateTime LastModified
        {
            get
            {
                Load();
                return sWebMetadata.LastModified;
            }
        }
        /// <summary>
        /// Guid of Website in SharePoint
        /// </summary>
        public Guid WebID
        {
            get { Load(); return new Guid(sWebMetadata.WebID); }
        }
        /// <summary>
        /// Description of Website
        /// </summary>
        public string Description
        {
            get { Load(); return sWebMetadata.Description; }
        }
        */
        /// <summary>
        /// Description of Website
        /// </summary>
        public string Description
        {
            get { Load(); return description; }
        }

        /// <summary>
        /// Return a Collection of all Lists in Website
        /// </summary>
        public ListCollection Lists
        {
            get
            {
                if (myLists == null) myLists = new ListCollection(myConnection,Uri);
                return myLists;
            }
        }
        public SiteCollection Sites
        {
            get 
            {
                Load();
                if (myCollection == null)
                {
                    site.Url = Uri.ToString() + WebService;
                    site.Credentials = Credentials;
                    ProgressEventArgs p = myConnection.DoProgress("Loading SiteCollection \"" + Uri + "\" (" + Title + ")");
                    MetaDataElement = site.GetWebCollection();
                    /*            site.GetWeb(out sWebMetadata, out vWebs, out vLists, out vFPUrls, out  strRoles, out vRolesUsers, out vRolesGroups);
                                Guid = new Guid(sWebMetadata.WebID);
                                myCollection = new SiteCollection(myConnection,vWebs);
                                title=sWebMetadata.Title;
                     */
                    XmlNodeList weblist = Select("/wss:Webs/wss:Web");
                    string[] urls = new string[weblist.Count];
                    string[] titles = new string[weblist.Count];
                    for (int i = 0; i < weblist.Count; i++)
                    {
                        urls[i] = weblist[i].Attributes["Url"].Value;
                        titles[i] = weblist[i].Attributes["Title"].Value;
                    }
                    myCollection = new SiteCollection(myConnection, titles, urls);
                    if (myLists != null)
                    {
                        myLists.Dispose();
                        myLists = null;
                    }
                    NeedRefresh = false;
                    p.Operation="Loaded Sites '" + Title + "' (" + myCollection.Count + ")";
                    p.State= BeforeOrAfter.After;
                    myConnection.UpdateProgress(p);
                    
                } 
                return myCollection;
            }
        }
       
        public override string ToString()
        {
            if (HasNoData) return base.ToString();
            else return Title;
        }
     
        #region IDisposable Member

        public void Dispose()
        {
            if (myLists!=null) myLists.Dispose();
            site.Dispose();
        }

        #endregion
    }
}
